<?php

namespace App\Models;

use App\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

class UserMoneyLog extends Model
{
    const DEPOSIT = 1; //充值
    const WITHDRAW = 2; //提现
    const TRANSFER_IN = 3; //转入
    const TRANSFER_OUT = 4; //转出
    const PRIZE_DEPOSIT = 5; //充值提成
    const CONSUME = 6; //消费
    const REVERSAL = 99; //冲正

    use HasFactory, HasDateTimeFormatter;

    protected $table = 'users_money_log';

    protected $casts =[
        'category' => 'integer',
        'money' => 'float',
    ];

    protected $appends = [
        'category_name',
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * 获取锁定金额
     */
    protected function categoryName(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                $def = [
                    self::DEPOSIT => '充值',
                    self::WITHDRAW => '提现',
                    self::TRANSFER_IN => '转入',
                    self::TRANSFER_OUT => '转出',
                    self::PRIZE_DEPOSIT => '充值提成',
                    self::CONSUME => '消费',
                    self::REVERSAL => '冲正'
                ];

                return $def[$attributes['category']] ?? '';
            },
        );
    }
}
